package com.binance.healthcenter.telegram.command;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.telegram.telegrambots.meta.api.objects.Update;

import com.binance.healthcenter.masterelection.MasterElection;
import com.binance.healthcenter.notification.NotificationStrategyService;
import com.binance.healthcenter.notification.TelegramNotificationService;
import com.binance.healthcenter.service.DiscoveryClientService;

import lombok.extern.log4j.Log4j2;

@Log4j2
@TelegrambotCommandMatcher("/mute_*")
public class MuteCommand implements TelegrambotCommand {
    @Autowired
    private TelegramNotificationService telegramNotificationService;
    @Autowired
    private MasterElection masterElection;
    @Autowired
    private DiscoveryClientService discoveryClientService;
    @Autowired
    private NotificationStrategyService notificationStrategyService;

    @Override
    public void execute(Update update) {
        String text = update.getMessage().getText();
        try {
            // 解析指令
            log.info("MuteCommand.execute :text={}", text);
            List<String> serviceNameList = discoveryClientService.getServiceNameList();
            String currentServiceName = null;
            String requestInfo = text.split("@")[0].replace("/mute_", "").trim();
            String requestServiceName=requestInfo.split("_")[0];
            String requestCommandInfo=requestInfo.split("_")[1];
            log.info("MuteCommand.execute start:text={},requestServiceName={},requestCommandInfo={}", text, requestServiceName,requestCommandInfo);
            for (String serviceName : serviceNameList) {
                if (serviceName.equalsIgnoreCase(requestServiceName)) {
                    currentServiceName = serviceName.toLowerCase();
                }
                if("all".equalsIgnoreCase(requestServiceName)){
                    currentServiceName="all";
                    break;
                }
            }
            // 无效的命令，不处理
            if (StringUtils.isBlank(currentServiceName)) {
                log.info("MuteCommand.execute start:text={} empty", text);
                return;
            }
            log.info("MuteCommand.execute start:text={},currentServiceName={},requestCommandInfo={}", text, currentServiceName,requestCommandInfo);
            // 开始写入某个禁止指令
            if("all".equalsIgnoreCase(currentServiceName)){
                notificationStrategyService.clearUpAllMuteCommandInfo();
            }else if("clearup".equalsIgnoreCase(requestCommandInfo)){
                notificationStrategyService.clearMuteCommandInfo(currentServiceName,requestCommandInfo);
            }else {
                notificationStrategyService.setMuteCommandInfo(currentServiceName,requestCommandInfo);
            }
            log.info("MuteCommand.execute end:text={},currentServiceName={},requestCommandInfo={}", text, currentServiceName,requestCommandInfo);
            // 当前机器是master的话那么收到指令，我们就推送信息
            if (masterElection.isMaster()) {
                String response="command: "+text+" is completed";
                telegramNotificationService.sendTextMessage(response);
            }
        } catch (Exception e) {
            log.error("MuteCommand.execute text=" + text + " exception:", e);
        }
    }

}
